#include "stdafx.h"
#include "DeLine.h"
#include "ColorDef.h"
#include "DeLineDialog.h"


DeLine::DeLine(void)
{
}


DeLine::DeLine(CWnd* pParent)
	: Graphic(pParent)
	, connectFlag_(-1)
{
	lineData_.width_ = 2;
	lineData_.color_ = COLOR_CYAN;
}


DeLine::~DeLine(void)
{
}


IMPLEMENT_SERIAL(DeLine, Graphic, 1)


void DeLine::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{	// storing code
		MapWindowPoints(GetParent(), lineData_.pt_, 2);
		ar.Write(&lineData_, sizeof(lineData_));
	}
	else
	{	// loading code
		ar.Read(&lineData_, sizeof(lineData_));
	}
}


BEGIN_MESSAGE_MAP(DeLine, Graphic)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void DeLine::OnPaint()
{
	CPaintDC dc(this); 
	CPen pen(PS_SOLID, lineData_.width_, lineData_.color_);
	dc.SelectObject(&pen);

	dc.MoveTo(lineData_.pt_[0]);
	dc.LineTo(lineData_.pt_[1]);

	drawConnectPt(dc, lineData_.pt_, connectFlag_);
}


void DeLine::setWindow(const CPoint& pt1, const CPoint& pt2)
{
	lineData_.pt_[0] = pt1;
	lineData_.pt_[1] = pt2;

	setWindow();
}


void DeLine::setWindow()
{
	CRect wndRect(lineData_.pt_[0], lineData_.pt_[1]);
	wndRect.NormalizeRect();
	wndRect.InflateRect(lineData_.width_, lineData_.width_);
	MoveWindow(wndRect, TRUE);

	GetParent()->MapWindowPoints(this, lineData_.pt_, 2);
}


void DeLine::compressWindow() 
{
	CPoint wndPt[4];
	getBorder(lineData_.pt_[0], lineData_.pt_[1], wndPt, lineData_.width_ + 2);
	setCRgnWindow(wndPt, 4);
}


bool DeLine::setAttr()
{
	DeLineDialog dlg(GetParent());
	dlg.setLine(this);
	return dlg.DoModal() == IDOK;
}


bool DeLine::OnMouseViewMove(const CPoint& viewPt, CPoint& outPt)
{
	return autoConnect(viewPt, connectFlag_, outPt, lineData_.pt_, 2);
}

